//
//  OO_AdressMenageViewController.m
//  ShopProject
//
//  Created by macbookpro on 2018/9/28.
//  Copyright © 2018年 ShopProject. All rights reserved.
//

#import "OO_AdressMenageViewController.h"
#import "OO_AddAddressViewController.h"
#import "OO_AddressTableViewCell.h"
#import "OO_EditAddressViewController.h"
@interface OO_AdressMenageViewController ()<UITableViewDelegate,UITableViewDataSource,OO_AddressTableViewCellDelgate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;

@end

@implementation OO_AdressMenageViewController
-(NSMutableArray *)dataSource{
    
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
    
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    WeakSelf
    /*只有一组数据，不要上拉加载*/
    [self.tableView setRefreshWithHeaderBlock:^{
        [weakSelf loadData ];
    } footerBlock:nil];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView.mj_header beginRefreshing];
    [self.tableView setupEmptyDataText:@"没有数据,点击重试" verticalOffset:40 emptyImage:[UIImage imageNamed:@"nodata"] tapBlock:^{
        [weakSelf.tableView.mj_header beginRefreshing ];
    }];
    self.title = @"收货地址管理";
    self.addBtn.backgroundColor = MainColor;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  
}
- (IBAction)addAddressButtonPress:(UIButton *)sender {
    OO_AddAddressViewController *addVc = [OO_AddAddressViewController new];
    WeakSelf
    addVc.addSuccess = ^{
        [weakSelf.tableView.mj_header beginRefreshing];
    };
    [self.navigationController pushViewController:addVc animated:1];
    
    
    
}

-(void)loadData{
    
    WeakSelf
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        JQFMDB *db = [JQFMDB shareDatabase];

        [weakSelf.dataSource removeAllObjects];
        
        [weakSelf.dataSource addObjectsFromArray: [db jq_lookupTable:ADDRESSDATABASE dicOrModel:[OO_AddressModel class] whereFormat:@"where zhangHao = '%@'",[UserDefaults readFromPlist:LOGIN_STATUS_TEL]]];
        weakSelf.dataSource =(NSMutableArray *)[[weakSelf.dataSource reverseObjectEnumerator] allObjects];
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.tableView reloadData];
            [weakSelf.tableView.mj_header endRefreshing];
            
            
        });
        
        
    });
    
    
    
}
#pragma mark - UITableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    OO_AddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OO_AddressTableViewCell"];
    if (!cell) {
        cell = [[OO_AddressTableViewCell alloc] initWithStyle:0 reuseIdentifier:@"OO_AddressTableViewCell"];
    }
    cell.model = self.dataSource[indexPath.row];
    cell.delgate = self;
    [cell useCellFrameCacheWithIndexPath:indexPath tableView:tableView];
    
    return cell;
    
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return  self.dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    // >>>>>>>>>>>>>>>>>>>>> * cell自适应 * >>>>>>>>>>>>>>>>>>>>>>>>
    OO_AddressModel *model = self.dataSource[indexPath.row];
    return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[OO_AddressTableViewCell class] contentViewWidth:[UIScreen mainScreen].bounds.size.width];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.selectedAddress) {
        self.selectedAddress(self.dataSource[indexPath.row]);
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    
    
    
    
}
-(void)editButtonClickedAtCell:(UITableViewCell *)cell{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    if (indexPath) {
        
        OO_AddressModel *model = self.dataSource[indexPath.row];
        
        OO_EditAddressViewController *vc = [OO_EditAddressViewController new];
        WeakSelf
        vc.deleteSuccess = ^{
            [weakSelf.tableView.mj_header beginRefreshing];
        };
        
        vc.editSuccess = ^{
            [weakSelf.tableView.mj_header beginRefreshing];

        };
        vc.model = model;
        [self.navigationController pushViewController:vc animated:1];
        
        
        
        
        
    }
    
    
}

@end
